package ddd2;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Random;
import java.util.Scanner;

public class GetSpeed {
    public static void main(String[] args) {
        String Str = generateRandomSentence();
        System.out.println("请输入以下内容：");
        System.out.println(Str);
        LocalDateTime now = LocalDateTime.now();
        Scanner sc = new Scanner(System.in);
        String TheStr = sc.nextLine();
        double cent = calculateCharAccuracy(Str, TheStr);
        String theCent = String.format("%.1f", cent*100);
        System.out.println("==== 测试结果 ====");
        System.out.println("正确率："+ theCent +"%");
        System.out.println("用时："+ Duration.between(now, LocalDateTime.now()).getSeconds()+"秒！");
        System.out.println("速度："+ 60*(Str.length() / Duration.between(now , LocalDateTime.now()).getSeconds())+"字/分钟");
        if(cent == 1){
            System.out.println("！！@#￥￥完美命中！！@#￥￥");
        }
    }

    public static String generateRandomSentence() {
        String[] subjects = {"The cat", "A student", "My friend", "The robot", "An artist"};
        String[] verbs = {"eats", "writes", "jumps over", "builds", "reads"};
        String[] objects = {"a book", "the wall", "a sandwich", "a poem", "the program"};

        Random rand = new Random();

        String subject = subjects[rand.nextInt(subjects.length)];
        String verb = verbs[rand.nextInt(verbs.length)];
        String object = objects[rand.nextInt(objects.length)];

        return subject + " " + verb + " " + object + ".";
    }
    public static double calculateCharAccuracy(String target, String input){
        int count = 0;
        int minSize;
        if(input.length() > target.length()){
            minSize = target.length();
        }else {
            minSize = input.length();
        }
        for(int i = 0; i < minSize; i++){
            if(target.charAt(i) == input.charAt(i)){
                count++;
            }
        }
        return count / (double) target.length();
    }
}
